class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user&.authenticate(params[:session][:password])
      if user.activated?
        log_in user
        flash[:success] = "登录成功！"
        redirect_to root_url()
      else
        message = "账户未激活。"
        message += "请查看您的邮箱中的激活链接"
        flash[:fail] = message
        redirect_to root_url()
        end
    else
      flash.now[:danger] = '邮箱地址或密码错误'
      redirect_to login_url()
    end
  end

  def destroy
    log_out
    flash[:success] = "已登出"
    redirect_to root_url()
  end
end
